package com.liruo.treasureattic.common.util;

import cn.hutool.core.collection.CollectionUtil;
import com.liruo.treasureattic.common.httpmsg.exception.CommonResultException;
import com.liruo.treasureattic.common.httpmsg.exception.ValidatorException;
import com.liruo.treasureattic.common.httpmsg.result.Errno;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * @Author:liruo
 * @Date:2022-10-31-21:45:42
 * @Desc   暂时只用到service
 */
public class ValidatorUtil {
    public static void dbOpt(boolean opt, String msg){
        if(!opt){
            throw new CommonResultException(Errno.SYSTEM, msg);
        }
    }
    public static void anyCollectionEmpty(String msg, Collection... collections){
        for (Collection collection : collections){
            if(CollectionUtil.isEmpty(collection)){
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
    }
    public static void allCollectionEmpty(String msg, Collection... collections){
        int cnt = 0;
        for (Collection collection : collections){
            if(CollectionUtil.isEmpty(collection)){
                cnt++;
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
        if(cnt == collections.length){
            throw new ValidatorException(Errno.PARAMS, msg);
        }
    }
    public static void anyMapEmpty(String msg, Map... maps){
        for (Map map : maps){
            if(CollectionUtil.isEmpty(map)){
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
    }
    public static void allMapEmpty(String msg, Map... maps){
        int cnt = 0;
        for (Map map : maps){
            if(CollectionUtil.isEmpty(map)){
                cnt++;
            }
        }
        if(cnt == maps.length){
            throw new ValidatorException(Errno.PARAMS, msg);
        }
    }


    public static void anyNull(Object... objs){
        anyNullThrowErr(null,null, objs);
    }
    public static void anyNull(String[] str, Object... objs){
        anyNullThrowErr(str,null,  objs);
    }
    public static void anyNull(String msg, Object... objs){
        anyNullThrowErr(null, msg, objs);
    }
    public static void anyNull(String[] str, String msg, Object... objs){
        anyNullThrowErr(str, msg, objs);
    }
    private static void anyNullThrowErr(String[] str, String msg, Object... objs){
        if(objs != null && ObjectUtils.anyNull(objs)){
            if(msg == null){
                throw new ValidatorException(Errno.PARAMS);
            }else{
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
        if(str == null){
            return;
        }
        if(StringUtils.isAnyEmpty(str)){
            if(msg == null){
                throw new ValidatorException(Errno.PARAMS);
            }else{
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
    }


    public static void allNull(Object... objs){
        anyNullThrowErr(null,null, objs);
    }
    public static void allNull(String[] str, Object... objs){
        allNullThrowErr(str,null,  objs);
    }
    public static void allNull(String msg, Object... objs){
        allNullThrowErr(null, msg, objs);
    }
    public static void allNull(String[] str, String msg, Object... objs){
        allNullThrowErr(str, msg, objs);
    }
    private static void allNullThrowErr(String[] str, String msg, Object... objs){
        boolean notAllNull = true;
        if(objs != null && ObjectUtils.allNull(objs)){
            notAllNull = false;
        }
        if(str == null){
            if(!notAllNull){
                if(msg == null){
                    throw new ValidatorException(Errno.PARAMS);
                }else{
                    throw new ValidatorException(Errno.PARAMS, msg);
                }
            }
            return;
        }
        if(notAllNull && StringUtils.isAllEmpty(str)){
            if(msg == null){
                throw new ValidatorException(Errno.PARAMS);
            }else{
                throw new ValidatorException(Errno.PARAMS, msg);
            }
        }
    }


    public static void checkLen(String str,int min,  int max){
        if(str == null || str.length() < min  || str.length() > max){
           throw  new ValidatorException(Errno.PARAMS,
                   "str=" + str
                   + " , make sure it len  in [" + min + "," + max + "]");
        }
    }
    public static void requireGreat0(Long... longs){
        if(longs == null || Arrays.stream(longs).anyMatch(l -> l == null || l <= 0)){
            throw new ValidatorException(Errno.SYSTEM, "some require greater than 0");
        }
    }
    public static void requireGreat0(Collection<Long> longs){
        if(longs == null || longs.stream().anyMatch(l -> l == null || l <= 0)){
            throw new ValidatorException(Errno.SYSTEM, "some require greater than 0");
        }
    }


    public static void checkUserName(String un, int min, int max){
        checkName(un,RegexUtil.USERNAME,min,max);
    }
    public static void checkCommonName(String name, int min,  int max) {
        checkName(name,RegexUtil.COMMON_NAME,min,max);
    }
    private static void checkName(String name, String regex, int min, int max) {
        checkLen(name, min, max);
        if(!name.matches(regex)){
            throw new ValidatorException(Errno.PARAMS, "require the name comply with the requirements");
        }
    }
    public static void checkPwd(String pwd, int min, int max){
        checkLen(pwd, min, max);
        if(!pwd.matches(RegexUtil.PASSWORD)){
            throw new ValidatorException(Errno.PARAMS, "require the password comply with the requirements");
        }
    }
    public static void checkEmail(String email){
        if(!StringUtils.isEmpty(email) && !email.matches(RegexUtil.EMAIL)){
            throw new ValidatorException(Errno.PARAMS, "require the email comply with the requirements");
        }
    }
}
